// pages/index1/index.js
const app = getApp()
// 引入SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js')

// 定义经纬度
var lati = null
var longi = null
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showInput: false,
    //4位密码获取焦点
    num_one: false,
    num_two: false,
    num_three: false,
    num_four: false,
    nums:[null,null,null,null],
    nowLocal:'获取中...',
    // 首页轮播图
    imgUrls: ['/imgs/banner.png', 
              '/imgs/banner02.png', 
              '/imgs/banner03.png'],
    indicatorDots: true, //是否显示面板指示点 
    autoplay: true, //是否自动切换 
    interval: 4000, //自动切换时间间隔 
    duration: 1000, //滑动动画时长 
    hasUserInfo: true,
    localFail: false
  },
  joinCircle: function(){
    this.setData({
      showInput: true,
      num_one: true,
    })
  },
  onClose: function(){
    this.setData({
      showInput: false
    })
  },
  //4位房间密码处理
  isDone: function(){
    // console.log(this.data.nums)
    let flag = true
    this.data.nums.forEach(function (n) {
      if(!n){
        flag = false
      }
    })
    return flag
  },
  getNumOne: function(e){
    if (e.detail.value == '' || e.detail.value == undefined){
      return
    }
    this.data.nums[0] = e.detail.value
    if(this.isDone()){
      this.checkPassNum()
      return
    }
    this.setData({
      num_one: false,
      num_two: true
    })
  },
  getNumTwo: function (e) {
    if (e.detail.value == '' || e.detail.value == undefined) {
      return
    }
    this.data.nums[1] = e.detail.value
    if (this.isDone()) {
      this.checkPassNum()
      return
    }
    this.setData({
      num_two: false,
      num_three: true
    })
  },
  getNumThr: function (e) {
    if (e.detail.value == '' || e.detail.value == undefined) {
      return
    }
    this.data.nums[2] = e.detail.value
    if (this.isDone()) {
      this.checkPassNum()
      return
    }
    this.setData({
      num_three: false,
      num_four: true
    })
  },
  getNumFur: function (e) {
    if (e.detail.value == '' || e.detail.value == undefined) {
      return
    }
    this.data.nums[3] = e.detail.value
    if (this.isDone()) {
      this.checkPassNum()
      return
    }
    this.setData({
      num_four: false
    })
  },
  checkPassNum: function(){
    this.setData({
      num_one: false,
      num_two: false,
      num_three: false,
      num_four: false
    })
    let str = ''
    this.data.nums.forEach(function(n){
      str += n
    })
    console.log(str)
    //4位码验证通过后
    this.setData({
      showInput: false
    })
    this.goOrder()
  },
   /**
   * 点击进入食堂点餐页
   */
  goOrder: function () {
    wx.navigateTo({
      url: '/pages/booking/booking',
    })
  },
  onGotUserInfo:function(){
    let _self = this
    wx.getUserInfo({
      success: function (res) {
        // console.log(res)        
        _self.setData({
          hasUserInfo: true
        })
        app.globalData.userInfo = res.userInfo
        app.globalData.userSecret.iv = res.iv
        app.globalData.userSecret.encryptedData = res.encryptedData
        app.register()
      }
    })
  },
  /**
   * 进入地址选择页面
   */
  goMap: function(){
    wx.navigateTo({
      url: '/pages/select_address/select_address',
    })
  },
  onShow: function () {
    let _self = this
    if (Object.keys(app.globalData.nowLocal).length === 0) {
      return 
    }
    // console.log(app.globalData.isChooseAd)
    if (app.globalData.isChooseAd) {      
      _self.setData({
        nowLocal: app.globalData.chooseLocal.localName
      })
      _self.getCanteen(app.globalData.chooseLocal)
    }else{
      _self.setData({
        nowLocal: app.globalData.nowLocal.localName
      })
      _self.getCanteen(app.globalData.nowLocal)
    }
  },
  onLoad(option){
    var _self = this       
    app.localNameReadyCallback = function () {
      _self.setData({
        nowLocal: app.globalData.nowLocal.localName
      })
      _self.getCanteen(app.globalData.nowLocal)
    }
    app.noLocalCallback = function () {
      _self.setData({
        localFail: true
      })
    }
    app.userInfoReadyCallback = function (res) {
      // console.log(app.globalData.userInfo)      
      if (!app.globalData.userInfo) {     
        _self.setData({
          hasUserInfo: false
        })
      }else{
        app.register()
      }
    }
  },
  setting:function(){
    let that = this
    wx.openSetting({
      success: function (res) {
        wx.getLocation({
          type: 'gcj02', //返回可以用于wx.openLocation的经纬度
          success: function (res) {
            // 调用接口, 坐标转具体位置 -xxz0717
            lati = res.latitude
            longi = res.longitude

            // 实例化API核心类
            var demo = new QQMapWX({
              key: 'OMYBZ-RE4HF-H6NJD-NM3DP-I7DQH-XHFLG' // 申请的key
            })
            // 调用接口
            demo.reverseGeocoder({
              location: {
                latitude: lati,
                longitude: longi
              },
              success: function (res) {
                app.globalData.localName = res.result.formatted_addresses.recommend
                that.setData({
                  nowLocal: res.result.formatted_addresses.recommend
                })
              }
            })
            console.log(res);
          }
        })  
        that.setData({
          localFail: false
        })
      }
    })
  },
  /**
   * 获取周围食堂信息
   */
  getCanteen: function(local){
    //显示loading 获取后loading隐藏
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(function(){
      wx.hideLoading()
    },1000)
    console.log('获取此坐标周围食堂信息',local)
  },
  /**
   * 下拉刷新食堂
   */
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh()
    // 请求数据方法位置一
    let _self = this
    setTimeout(function () {
      // 请求数据方法位置二
      _self.getCanteen()
    }, 500);
  }
})